﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sora.HelpSystem.Editor.Logic;
using System.ComponentModel;
using Microsoft.Win32;
using Sora.HelpSystem.Package.Pages;

namespace Sora.HelpSystem.Editor
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private HelpDocumentEditorRoot _editorRoot;

        public HelpDocumentEditorRoot EditorRoot
        {
            get { return _editorRoot; }
            private set
            {
                _editorRoot = value;
                RaisePropertyChanged("EditorRoot");
            }
        }

        private string _currentFile;

        public string CurrentFile
        {
            get { return _currentFile; }
            set
            {
                _currentFile = value;
                RaisePropertyChanged("CurrentFile");
            }
        }

        public MainWindow()
        {
            EditorRoot = new HelpDocumentEditorRoot(new Package.HelpDocument());
            EditorRoot.Document.ReadSource();

            InitializeComponent();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }

        #endregion
        
        private void menuSave_Click(object sender, RoutedEventArgs e)
        {
            treeView.Focus();

            if (String.IsNullOrEmpty(CurrentFile))
                menuSaveAs_Click(null, null);
            else
            {
                EditorRoot.Document.SaveAs(CurrentFile, true);
            }
        }

        private void menuSaveAs_Click(object sender, RoutedEventArgs e)
        {
            treeView.Focus();

            var dlg = new SaveFileDialog();
            dlg.Filter = "Fichier d'aide (*.dshelp)|*.dshelp|Tous les fichiers|*.*";

            if (dlg.ShowDialog() == true)
            {
                EditorRoot.Document.SaveAs(dlg.FileName, true);
                CurrentFile = dlg.FileName;
            }
        }

        private void menuOpen_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new OpenFileDialog();
            dlg.Filter = "Fichier d'aide (*.dshelp)|*.dshelp|Tous les fichiers|*.*";

            if (dlg.ShowDialog() == true)
            {
                EditorRoot = new HelpDocumentEditorRoot(new Package.HelpDocument(dlg.FileName, false, true));
                EditorRoot.Document.ReadSource();
                CurrentFile = dlg.FileName;
            }
        }

        private void menuTreeNewSubPage_Click(object sender, RoutedEventArgs e)
        {
            var selected = treeView.SelectedItem as HelpPage;

            if (selected != null)
            {
                NewPageToTarget(selected);
            }
        }

        private void NewPageToTarget(HelpPage target)
        {
            string newname = "new-page";

            for (int i = 2; target.ChildExists(newname); i++)
            {
                newname = String.Format("new-page-({0})", i);
            }

            target.Child(newname, true);
        }

        private void menuTreeNewLevelPage_Click(object sender, RoutedEventArgs e)
        {
            var target = EditorRoot.HelpRoot;

            var selected = treeView.SelectedItem as HelpPage;

            if (selected != null)
            {
                if (selected.Parent != null)
                    target = selected.Parent;
            }

            NewPageToTarget(target);
        }

        private void menuTreeDeletePage_Click(object sender, RoutedEventArgs e)
        {
            var selected = treeView.SelectedItem as HelpPage;

            if (selected != null)
            {
                selected.Delete();
            }
        }

        private void treeView_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            menuTreeDeletePage.IsEnabled =
                menuTreeNewSubPage.IsEnabled =
                treeView.SelectedItem != null;
        }

        private void treeView_MouseDown(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem clickedItem = null;

            /* on recherche l'élément sous le click */
            DependencyObject obj = (DependencyObject)e.OriginalSource;

            while (obj != null && obj != treeView)
            {
                if (obj.GetType() == typeof(TreeViewItem))
                {
                    clickedItem = (TreeViewItem)obj;

                    break;
                }
                obj = VisualTreeHelper.GetParent(obj);
            }

            if (clickedItem != null)
            {
                clickedItem.IsSelected = true;
            }
            else
                treeView.SelectedValuePath = String.Empty;
        }

        private void menuGenerateSearch_Click(object sender, RoutedEventArgs e)
        {
            EditorRoot.DocumentProvider.SearchProvider.GenerateSearchIndex();
        }
    }
}
